<?php
namespace app\miniprogram\controller;

/**
 * 订单控制
 */
class Order extends Base
{
	protected $process = 'Order';
    protected $noNeedLogin = ['get_options', 'get_menu', 'order_timeout', 'order_automatic_delivery'];
 
    public function create() {
    	$this->ajax_process(function($process, $user) {
    		return $process->create($user);
    	});
    }

    public function cart() {
    	$this->ajax_process(function($process, $user) {
    		return $process->cart();
    	});
    }

    public function private_list() {
    	$this->ajax_process(function($process, $user) {
    		return $process->private_list($user);
    	});
    }

    public function store_list() {
        $this->ajax_process(function($process, $user) {
            return $process->store_list($user);
        });
    }

    public function get_options() {
        $field = $this->request->param('field');
        $enum = loadEnum('OrderStatus');
        $options = $enum->get_options();
        if($field) {
            $field = explode(',', $field);
            $arr = [];
            foreach($field as $k => $v) {
                $arr[$enum->code2key($v)] = $options[$enum->code2key($v)];
            }
            $this->success('', (object)$arr);
        } else {
            $this->success('', $options);
        }
    }

    public function get_menu() {
        $field = $this->request->param('field');
        $enum = loadEnum('OrderStatus');
        $menu = $enum->get_menu();
        $options = $enum->get_options();
        if($field) {
            $field = explode(',', $field);
            $arr = [];
            foreach($field as $k => $v) {
                if($enum->code2key($v) !== false) {
                    $arr[$enum->code2key($v)] = $options[$enum->code2key($v)];
                } else {
                    $arr[$v] = $menu[$v];
                }
            }
            $this->success('', $arr);
        } else {
            $this->success('', $menu);
        }
    }

    //订单详情
    public function preview_look() {
        $this->ajax_process(function($process, $user) {
            $args = $process->preview_look($user);
            $args['row']->append(['snapshot_address']);
            return $args;
        });
    }

    //根据订单编号获取订单详情
    public function preview_look4out_trade_no() {
        $this->ajax_process(function($process, $user) {
            $args = $process->preview_look4out_trade_no($user);
            if($args['row']) {
                $args['row']->append(['snapshot_address']);
            }
            return $args;
        });
    }

    //店铺查看订单详情
    public function preview_look_store() {
        $this->ajax_process(function($process, $user) {
            $args = $process->preview_look_store($user);
            if($args['row']) {
                $args['row']->append(['snapshot_address']);
            }
            return $args;
        });
    }

    //发货
    public function send_product() {
        $this->ajax_process(function($process, $user) {
            return $process->send_product($user);
        });
    }

    //收货
    public function harvest() {
        $this->ajax_process(function($process, $user) {
            return $process->harvest($user);
        });
    }

    //修改地址
    public function set_address() {
        $this->ajax_process(function($process, $user) {
            return $process->set_address($user);
        });
    }

    //取消订单
    public function reverse() {
        $this->ajax_process(function($process, $user) {
            return $process->reverse($user);
        });
    }

    //关闭超时订单
    public function order_timeout() {
        $this->ajax_process(function($process, $user) {
            return $process->order_timeout($user);
        });
    }

    //自动收货
    public function order_automatic_delivery() {
        $this->ajax_process(function($process, $user) {
            return $process->order_automatic_delivery($user);
        });
    }

    //用户申请退款
    public function refund() {
        $this->ajax_process(function($process, $user) {
            return $process->refund($user);
        });
    }

    
}
